package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

public class PjTerminationCharge implements ServerInterface {
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			PjInfoBean pjInfoBean = JsonUtils.objectToPojo(req.getData(), PjInfoBean.class);
			String checkResult = check(pjInfoBean);
			if(!"OK".equals(checkResult)) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, checkResult, null);
			}
			String refundWay = pjInfoBean.getRefundWay();
			if("10".equals(refundWay)){
				factory.getMapper(PjInfoMapper.class).updateComments(pjInfoBean.getComments(),pjInfoBean.getId());
			}
			pjInfoBean.setUpdateName(userInfo.getUserInfo().getName());
			pjInfoBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
			factory.getMapper(PjInfoMapper.class).saveRefund(pjInfoBean);
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", null);
		}catch(Exception e){
			logger.error("工程终止结算收款失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "工程终止结算收款失败", null);
		}
	}

	private String check(PjInfoBean pjInfoBean) {
		if(pjInfoBean.getSetmtPayDate() == null) {
			return  "结算日期不能为空。";
		}
		if(pjInfoBean.getRefundWay() == null) {
			return  "余额退款方式不能为空。";
		}
		if("5".equals(pjInfoBean.getRefundWay())) {
			return  "工程终止余额退款不能转余额。";
		}
		return "OK";
	}
}
